package com.mmy.fta.trade.web.rest;

import com.mmy.fta.trade.server.dto.CompletedOrderDTO;
import com.mmy.fta.trade.server.dto.TransportOrderDTO;
import com.mmy.fta.trade.server.request.CompletedOrderRequest;
import com.mmy.fta.trade.server.request.PayCargoFeeRequest;
import com.mmy.fta.trade.server.response.CreatOrderResponse;
import com.mmy.fta.trade.server.request.TransportOrderRequest;
import com.mmy.fta.trade.server.request.CreatOrderRequest;
import com.mmy.fta.trade.server.response.PayCargoFeeResponse;
import com.mmy.fta.trade.server.service.OrderService;
import com.mmy.fta.trade.server.service.PaymentService;
import com.mmy.fta.trade.web.request.PaymentWebRequest;
import com.mmy.fta.trade.web.response.CompletedOrderWebResponse;
import com.mmy.fta.trade.web.response.TransportOrderWebResponse;
import com.ymm.common.web.BaseData;
import com.ymm.common.web.YmmResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * @apiNote 支付订金
 * @author qiang.lin
 * @since 2021/10/29
 */

@RestController
@RequestMapping(value = "/pay")
@Slf4j
public class PaymentController {
    @Autowired
    PaymentService paymentService;

    @Autowired
    OrderService orderService;

    // todo
    @PostMapping(value = "/payment")
    public YmmResponse<BaseData> payDeposit(@RequestBody PaymentWebRequest paymentWebRequest) {
        if(paymentWebRequest == null){
            log.info("paymentWebRequest 参数不正确");
            return YmmResponse.failResponse(1,"请求参数不正确");
        }

        if(paymentWebRequest.getPayMode() == 101){
            CreatOrderRequest creatOrderRequest = new CreatOrderRequest();
            BeanUtils.copyProperties(paymentWebRequest,creatOrderRequest);
            CreatOrderResponse creatOrderResponse = orderService.createTransportOrderByDriver(creatOrderRequest);

            boolean creatOrderByDriver = creatOrderResponse.getIsSuccess();
            if(creatOrderByDriver){
                List<TransportOrderDTO> orders = orderService.queryTransportOrdersByDriver(new TransportOrderRequest(creatOrderRequest.getOpenId()));

                TransportOrderWebResponse transportOrderWebResponse = new TransportOrderWebResponse();
                transportOrderWebResponse.setOrders(orders);

                return YmmResponse.succResponse(transportOrderWebResponse);
            }else {
                log.info("payType");
                return YmmResponse.failResponse(0,"支付失败");
            }
        }
        log.info("paymentDeposit 失败");
        return YmmResponse.failResponse(0,"失败");
    }


    /**
     * 货主支付运费
     * @param paymentWebRequest
     * @return
     * @throws IOException
     */
    @PostMapping("/payCargoFeeByShipper")
    public YmmResponse<BaseData> payCargoFee(@RequestBody PaymentWebRequest paymentWebRequest) throws IOException {
        if(paymentWebRequest == null){
            log.info("paymentWebRequest 参数不正确");
            return YmmResponse.failResponse(1,"请求参数不正确");
        }

        if(paymentWebRequest.getPayMode() == 201){
            PayCargoFeeRequest payCargoFeeRequest = new PayCargoFeeRequest();
            BeanUtils.copyProperties(paymentWebRequest,payCargoFeeRequest);
            PayCargoFeeResponse payCargoFeeResponse =paymentService.payCargoFeeByShipper(payCargoFeeRequest);

            boolean payCargoFeeIsSuccess = payCargoFeeResponse.isSuccess();

            if(payCargoFeeIsSuccess){
                List<CompletedOrderDTO> orders = orderService.getCompletedOrdersByShipper(new CompletedOrderRequest(paymentWebRequest.getOpenId()));


                CompletedOrderWebResponse completedOrderWebResponse = new CompletedOrderWebResponse();
                completedOrderWebResponse.setOrders(orders);

                return YmmResponse.succResponse(completedOrderWebResponse);
            }else {
                log.info("货主支付运费 失败");
                return YmmResponse.failResponse(0,"支付失败");
            }
        }
        log.info("货主支付运费 失败");
        return YmmResponse.failResponse(0,"支付失败");
    }

}